#//@reference Agile Web Development with Rails (4th Edition) (Kiely Breda x11105976)

class Product < ActiveRecord::Base
  belongs_to :user
 has_many :line_items

 before_destroy :ensure_not_referenced_by_any_line_item


   def self.search(search_query)
     if search_query
        find(:all,:conditions => ['name LIKE ?', "%#{ search_query}%"])
     else
       find(:all)
    end
  end
end

private
 # ensure that there is no line item referencing this product
   def ensure_not_referenced_by_any_line_item
     if line_items.empty?
       true
     else
       errors.add(:base, 'Line Items present')
       false
       end

     end
